/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iwords.web.teacher;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;

/**
 *
 * @author yunzi
 */
public abstract class BaseServlet<T> extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String command = request.getParameter("command");
            System.out.println("BaseServlet.processRequest() 1 command = " + command);
            //System.out.println("BaseServlet.processRequest() 2 request.getRequestURI() = " + request.getRequestURI());
            if (command == null) {
                defaultAction(request, response);
            } else {
                System.out.println("BaseServlet.processRequest() 3 command = "+command);
                //System.out.println("BaseServlet.processRequest() 4 getClass() = "+getClass());
                Method commandDeclaredMethod = getClass().getDeclaredMethod(command, HttpServletRequest.class, HttpServletResponse.class);
                //System.out.println("BaseServlet.processRequest() 5 commandDeclaredMethod = "+commandDeclaredMethod);
                //Method commandMethod = getClass().getMethod(command, HttpServletRequest.class, HttpServletResponse.class);
                //System.out.println("BaseServlet.processRequest() 6 commandMethod = "+commandMethod);
                commandDeclaredMethod.invoke(this, request, response);
            }
        } catch (NoSuchMethodException nsme) {
            //nsme.printStackTrace();
            defaultAction(request, response);
        } catch (Exception e) {
            error(request, response, e.getMessage());
            e.printStackTrace();
        }
    }

    /**如果command不能match到任何方法,默认显示的页面*/
    protected void defaultAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        error(request, response, "command参数指定的操作不存在。");
    }
    protected void error(HttpServletRequest request, HttpServletResponse response, String message) throws ServletException, IOException {
        String path = "/error.jsp";
        System.out.println("BaseServlet.error() path = " + path + "message = "+message);
        request.setAttribute("errMsg", message);
        getServletContext().getRequestDispatcher(path).forward(request, response);
    }

    protected void forward(HttpServletRequest request, HttpServletResponse response, String url) throws ServletException, IOException {
        String path = url;
        System.out.println("MobileServlet.forward() path = " + path);
        getServletContext().getRequestDispatcher(path).forward(request, response);
    }

    /** 
     *  
     * @param request存储着表单的HttpServletRequest对象 
     * @param bean要封装的表单Bean 
     * @return 封装好的表单Bean 
     */
    public T parseRequest(HttpServletRequest request, T bean) {
        //取得所有参数列表  
        Enumeration enu = request.getParameterNames();
        //遍历所有参数列表  
        while (enu.hasMoreElements()) {

            Object obj =
                    enu.nextElement();
            try {
                //取得这个参数在Bean中的数据类开  
                Class cls = PropertyUtils.getPropertyType(bean, obj.toString());
                //把相应的数据转换成对应的数据类型  
                Object beanValue = ConvertUtils.convert(request.getParameter(obj.toString()), cls);
                //填充Bean值  
                PropertyUtils.setProperty(bean, obj.toString(), beanValue);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block  
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block  
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block  
                //e.printStackTrace();
            }
        }

        return bean;
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
